<template>
  <el-dialog
    title="用户列表"
    :visible.sync="show"
    width="80%"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-row :gutter="24">
        <el-col :span="6" :xs="24">
          <el-form-item label="所属机构" prop="findId">
            <college-selector
              v-model="queryParams.findId"
              :is-tree="true"
            ></college-selector>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="用户名称" prop="username">
            <el-input
              v-model="queryParams.username"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList">
      <el-table-column
        label="序号"
        align="center"
        width="70"
        type="index"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="所属学院"
        align="center"
        key="collegeName"
        prop="collegeName"
        v-if="columns[1].visible"
      >
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in scope.row.collegeName" :key="index">{{
            item
          }}</el-tag>
        </template></el-table-column
      >
      <el-table-column
        label="用户名称"
        align="center"
        key="username"
        prop="username"
        v-if="columns[2].visible"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="姓名"
        align="center"
        key="nickname"
        prop="nickname"
        v-if="columns[3].visible"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系方式"
        align="center"
        key="mobile"
        prop="mobile"
        v-if="columns[4].visible"
        width="120"
      />
      <el-table-column
        label="角色"
        align="center"
        key="roleid"
        prop="roleid"
        v-if="columns[7].visible"
        width="120"
      >
        <template slot-scope="scope">
          <el-tag v-for="item in scope.row.userRoleVoList" :key="item.id">{{
            item.name
          }}</el-tag>
        </template></el-table-column
      >
      <el-table-column
        label="状态"
        key="status"
        v-if="columns[5].visible"
        align="center"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        v-if="columns[6].visible"
        width="165"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="primary" @click="handleChange(scope.row)"
            >选择</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import {
  listUser,
} from "@/api/system/user";
import CollegeSelector from "@/components/CollegeSelector";
export default {
  name: 'userList',
  components: {
    CollegeSelector
  },
  props: {
    show: {
      type: Boolean,
      default: false,
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        username: undefined,
        mobile: undefined,
        status: undefined,
        findId: undefined,
        createTime: [],
      },
      // 列信息
      columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `所属学院`, visible: true },
        { key: 2, label: `用户名称`, visible: true },
        { key: 3, label: `姓名`, visible: true },
        { key: 4, label: `联系方式`, visible: true },
        { key: 5, label: `状态`, visible: false },
        { key: 6, label: `创建时间`, visible: true },
        { key: 7, label: `角色`, visible: true },
      ],
    }

  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleChange(row) {
      this.$emit('update:show', false);
      this.$emit("success", row);
    },
    cancel() {
      this.$emit("update:show", false);
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        username: undefined,
        mobile: undefined,
        status: undefined,
        deptId: undefined,
        createTime: [],
      };
      this.resetForm("form");
    }
  }
}

</script>

<style>
</style>